/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.telas;

import java.util.List;
import javax.swing.JOptionPane;
import sys.donto.usuario.Usuario;
import sys.donto.usuario.UsuarioDAO;
import sys.donto.usuario.UsuarioTableModel;
import sys.donto.util.Util;

/**
 *
 * @author JOABB
 */
public class telaUsuario extends javax.swing.JDialog {
    private Usuario usu;

    /**
     * Creates new form testeUsu
     */
    public telaUsuario() { //METODO CONSTRUTOR
        setModal(true);
        initComponents();
        setLocationRelativeTo(null);
        limpaCampos();
        
        setTitle("sysDONTO - Cadastro de Usuários"); //Define nome no titulo da tela
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tfNome = new javax.swing.JTextField();
        tfLogin = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tfSenha = new javax.swing.JPasswordField();
        tfConfirmar = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        btSair1 = new javax.swing.JButton();
        btNovo1 = new javax.swing.JButton();
        btSalvar1 = new javax.swing.JButton();
        btPesquisar1 = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLFundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setName(""); // NOI18N
        jPanel1.setLayout(null);

        tfNome.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfNomeActionPerformed(evt);
            }
        });
        jPanel1.add(tfNome);
        tfNome.setBounds(100, 120, 600, 20);

        tfLogin.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfLoginActionPerformed(evt);
            }
        });
        jPanel1.add(tfLogin);
        tfLogin.setBounds(100, 150, 210, 20);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("NOME*:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(50, 120, 50, 15);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("LOGIN*:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(50, 150, 50, 15);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("SENHA*:");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(40, 180, 60, 15);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Confirmar*");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(20, 210, 70, 15);

        tfSenha.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.add(tfSenha);
        tfSenha.setBounds(100, 180, 210, 20);

        tfConfirmar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.add(tfConfirmar);
        tfConfirmar.setBounds(100, 210, 210, 20);

        jLabel1.setText("* Campos Obrigatórios");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(40, 250, 160, 14);

        btSair1.setBackground(new java.awt.Color(0, 153, 153));
        btSair1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btSair1.setForeground(new java.awt.Color(255, 255, 255));
        btSair1.setMnemonic('S');
        btSair1.setText("SAIR");
        btSair1.setToolTipText("Sair");
        btSair1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSair1ActionPerformed(evt);
            }
        });
        jPanel1.add(btSair1);
        btSair1.setBounds(630, 250, 63, 30);

        btNovo1.setBackground(new java.awt.Color(0, 153, 153));
        btNovo1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btNovo1.setForeground(new java.awt.Color(255, 255, 255));
        btNovo1.setMnemonic('N');
        btNovo1.setText("NOVO");
        btNovo1.setToolTipText("Novo");
        btNovo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovo1ActionPerformed(evt);
            }
        });
        jPanel1.add(btNovo1);
        btNovo1.setBounds(260, 250, 70, 30);

        btSalvar1.setBackground(new java.awt.Color(0, 153, 153));
        btSalvar1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btSalvar1.setForeground(new java.awt.Color(255, 255, 255));
        btSalvar1.setMnemonic('S');
        btSalvar1.setText("SALVAR");
        btSalvar1.setToolTipText("Salvar");
        btSalvar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvar1ActionPerformed(evt);
            }
        });
        jPanel1.add(btSalvar1);
        btSalvar1.setBounds(340, 250, 83, 30);

        btPesquisar1.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisar1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btPesquisar1.setForeground(new java.awt.Color(255, 255, 255));
        btPesquisar1.setMnemonic('P');
        btPesquisar1.setText("PESQUISAR");
        btPesquisar1.setToolTipText("Pesquisar");
        btPesquisar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisar1ActionPerformed(evt);
            }
        });
        jPanel1.add(btPesquisar1);
        btPesquisar1.setBounds(430, 250, 103, 30);

        btExcluir.setBackground(new java.awt.Color(0, 153, 153));
        btExcluir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btExcluir.setForeground(new java.awt.Color(255, 255, 255));
        btExcluir.setMnemonic('E');
        btExcluir.setText("EXCLUIR");
        btExcluir.setToolTipText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });
        jPanel1.add(btExcluir);
        btExcluir.setBounds(540, 250, 85, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("USUÁRIO");
        jLabel3.setFocusable(false);
        jPanel1.add(jLabel3);
        jLabel3.setBounds(200, 40, 360, 40);

        jCheckBox1.setText("Administrador");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });
        jPanel1.add(jCheckBox1);
        jCheckBox1.setBounds(340, 210, 100, 23);

        jLFundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/telasPP.jpg"))); // NOI18N
        jPanel1.add(jLFundo);
        jLFundo.setBounds(0, 0, 740, 310);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void limpaCampos(){
        tfConfirmar.setText("");
        tfLogin.setText("");
        tfNome.setText("");
        tfSenha.setText("");
        
        usu=new Usuario();
        btExcluir.setEnabled(false);
    }
    private void tfLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfLoginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfLoginActionPerformed

    private void tfNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfNomeActionPerformed

    private void btSair1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSair1ActionPerformed
        dispose();
    }//GEN-LAST:event_btSair1ActionPerformed

    private void btNovo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovo1ActionPerformed
        limpaCampos(); //CHAMA METODO PARA LIMPAR CAMPOS
    }//GEN-LAST:event_btNovo1ActionPerformed

    private void btSalvar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvar1ActionPerformed
        if (Util.chkVazio(tfNome.getText(),tfConfirmar.getText(),tfLogin.getText(),tfSenha.getText())){
            if(tfSenha.getText().equals(tfConfirmar.getText())){// compara senha e salva se elas forem iguais.

        usu.setNome(tfNome.getText());
        usu.setLogin(tfLogin.getText());
        usu.setSenha(tfSenha.getText());
        

        UsuarioDAO dao = new UsuarioDAO();
        dao.salvar(usu);
        limpaCampos();

        JOptionPane.showMessageDialog(rootPane, "Cadastro de Usuario Efetuado com Sucesso!");
        } else{
                JOptionPane.showMessageDialog(rootPane, "Senha e Confirmação de Senha não Conferem!");
            }
            }
    }//GEN-LAST:event_btSalvar1ActionPerformed

    private void btPesquisar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisar1ActionPerformed
        limpaCampos();
        UsuarioDAO dao = new UsuarioDAO();
        List <Usuario> usuarios = (!tfNome.getText().isEmpty() ? dao.pesquisaDescricao(tfNome.getText())
            : dao.listar());

        UsuarioTableModel utm = new UsuarioTableModel(usuarios);

        Object o = telaPesquisa.exibeTela(utm, "Usuario");

        if (o != null){
            usu = new Usuario();
            usu = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));

            tfNome.setText(usu.getNome());
            tfLogin.setText(usu.getLogin());
            tfSenha.setText(usu.getSenha());
           
            btExcluir.setEnabled(true);
        }
    }//GEN-LAST:event_btPesquisar1ActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        if (JOptionPane.showConfirmDialog(rootPane,
            "Deseja excluir Usuário?",
            "Excluir?", JOptionPane.YES_NO_OPTION)
        == JOptionPane.YES_OPTION) {

        
        UsuarioDAO dao = new UsuarioDAO();
        dao.remover(usu);
        limpaCampos();

        JOptionPane.showMessageDialog(rootPane, "Usuario Excluído com Sucesso!");
        }else{
            limpaCampos();
        }
    }//GEN-LAST:event_btExcluirActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaUsuario().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo1;
    private javax.swing.JButton btPesquisar1;
    private javax.swing.JButton btSair1;
    private javax.swing.JButton btSalvar1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLFundo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField tfConfirmar;
    private javax.swing.JTextField tfLogin;
    private javax.swing.JTextField tfNome;
    private javax.swing.JPasswordField tfSenha;
    // End of variables declaration//GEN-END:variables
}
